//
//  MenuListCell.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/24.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "MenuCollectionCell.h"

#import "MenuListCell.h"

@interface MenuListCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>{
    CGSize _cellSize;
    UIEdgeInsets _collectionEdge;
}
@end

@implementation MenuListCell

+ (CGFloat)calculateHeight {

    CGFloat collectionWidth = TTScreenWidth ;
    CGFloat distance = 5;
    CGFloat simpleHeight = collectionWidth/2 - 2*distance;
    simpleHeight = simpleHeight + 2*distance + 55;

    return simpleHeight;
}

- (void)fillCellWithModel:(NSMutableArray <CommonMenuModel *>*)menuArray {

    self.menuArray = menuArray;
    
    [self.collectionview reloadData];
}

- (void)awakeFromNib {
    [super awakeFromNib];

    CGFloat width = TTScreenWidth ;
    
    CGFloat cellWidth;
    CGFloat distance = 5;
    cellWidth = width/2.5 - 2*distance;

    _cellSize =  CGSizeMake(cellWidth, 160);
    _collectionEdge = UIEdgeInsetsMake(distance, distance, distance, distance);
    
    UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc]init];
    flowLaout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    self.collectionview.collectionViewLayout = flowLaout;
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    self.collectionview.showsHorizontalScrollIndicator = NO;
    self.collectionview.showsVerticalScrollIndicator = NO;
    
    [self.collectionview registerNib:[UINib nibWithNibName:@"MenuCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"MenuCollectionCellID"];
}

- (void)fillCellWithModel {
    
    [self.collectionview reloadData];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MenuCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuCollectionCellID" forIndexPath:indexPath];
    [cell fillCellWithModel:self.menuArray[indexPath.row]];
    
    WEAKSELF
    cell.MenuCollectionLoginBlock = ^{
      
        if (weakSelf.MenuListCellLoginBlock) {
            weakSelf.MenuListCellLoginBlock();
        }
    };

    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.menuArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return _cellSize;
}

//设置cell的位置
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                       layout:(UICollectionViewLayout *)collectionViewLayout
       insetForSectionAtIndex:(NSInteger)section {

    return _collectionEdge;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (self.MenuListCellBlock) {
        self.MenuListCellBlock(indexPath.row);
    }
}

@end
